/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import dominio.Usuario;
import java.util.List;
import javax.persistence.Query;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author convidado
 */
public class AutenticacaoDao {

    public AutenticacaoDao() {
        emf = Persistence.createEntityManagerFactory("trabalhoAPAEPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public Usuario pesquisa(Usuario u ){
        EntityManager em = getEntityManager();
        List<Usuario> l = null;
        try {
            Query q = em.createQuery("SELECT u FROM Usuario u WHERE LOGIN = '"+u.getLogin()+"' AND SENHA = '"+u.getSenha()+"'");
            l = q.getResultList();
            em.close();
        }catch(Exception e){
            em.close();
            return null;  
        }
        
        if(l.isEmpty()){
            return null;
        }
        return l.get(0);

    }

}
